package com.ericsson.enap.modules.ebmLog.task.vgc;

import com.ericsson.common.util.DateUtils;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.config.datasource.DynamicDataSourceContextHolder;
import com.ericsson.config.enums.DataSourceType;
import com.ericsson.enap.modules.ebmLog.config.EbmParam;
import com.ericsson.enap.modules.ebmLog.entity.vgc.Dimension;
import com.ericsson.enap.modules.ebmLog.entity.vgc.VgcEbmCcScc;
import com.ericsson.enap.modules.ebmLog.entity.vgc.VgcEbmTaskParam;
import com.ericsson.enap.modules.ebmLog.entity.vgc.amf.*;
import com.ericsson.enap.modules.ebmLog.mapper.vgc.VgcEbmLogMapper;
import jodd.bean.BeanUtil;
import org.slf4j.Logger;
import org.springframework.beans.BeanUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

import static com.ericsson.common.util.DateUtils.parseDate;
import static com.ericsson.enap.modules.ebmLog.utils.VgcEbmLogUtil.*;
import static java.util.stream.Collectors.counting;

/**
 * @description: 5gc ebm多维核心统计任务
 * @author: wangjingshi
 * @create: 2021-11-10 15:40
 */
public class VgcEbmCoreTask implements Runnable{

    private Logger log = LoggerUtils.Logger(LogFileName.VgcEbmLogJob);
    private String netId;
    private String localPath;
    private String dateTime;
    private String eventType;
    private CountDownLatch inner_latch;
    private VgcEbmLogMapper vgcEbmLogMapper;
    private EbmParam ebmParam;
    private Map<String,Integer> dnnMap;
    private Map<String,Integer> srtypeMap;
    private Map<String,Integer> srtriggerMap;
    private Map<String,Integer> handoverMap;
    private Map<String,Integer> deregistartionMap;
    private Map<Integer,String> _dnnMap;
    private Map<Integer,String> _srtypeMap;
    private Map<Integer,String> _srtriggerMap;
    private Map<Integer,String> _deregistartionMap;
    private Map<String,Integer> resultTypeMap;

    public VgcEbmCoreTask(VgcEbmTaskParam vgcEbmTaskParam,String eventType,CountDownLatch inner_latch) {
        this.netId = String.valueOf(vgcEbmTaskParam.getNetId());
        this.localPath = vgcEbmTaskParam.getLocalPath();
        this.dateTime = vgcEbmTaskParam.getDateTime();
        this.eventType = eventType;
        this.inner_latch = inner_latch;
        this.vgcEbmLogMapper = vgcEbmTaskParam.getVgcEbmLogMapper();
        this.ebmParam = vgcEbmTaskParam.getEbmParam();
        this.dnnMap = vgcEbmTaskParam.getDnnMap();
        this.srtypeMap = vgcEbmTaskParam.getSrtypeMap();
        this.srtriggerMap = vgcEbmTaskParam.getSrtriggerMap();
        this.handoverMap = vgcEbmTaskParam.getHandoverMap();
        this.deregistartionMap = vgcEbmTaskParam.getDeregistartionMap();
        this._dnnMap = vgcEbmTaskParam.get_dnnMap();
        this._srtypeMap = vgcEbmTaskParam.get_srtypeMap();
        this._srtriggerMap = vgcEbmTaskParam.get_srtriggerMap();
        this._deregistartionMap = vgcEbmTaskParam.get_deregistartionMap();
        this.resultTypeMap = vgcEbmTaskParam.getResultTypeMap();

    }

    @Override
    public void run() {
        try {
            localPath = "C:\\Users\\ws_wa\\Desktop\\csv\\";
            String[] dirs = new File(localPath).list((x,y)-> y.endsWith(eventType+".csv"));
            List<VgcEbmCcScc> ccSccList = new ArrayList<>();
            List<Dimension> dimensionList = new ArrayList<>();
            List<VgcEbmCcScc> tacNciList = new ArrayList<>();
            VgcEbmCcScc vgcEbmCcScc;
            Dimension dimension;
            for(int i=0;i<dirs.length;i++){
                InputStreamReader read = new InputStreamReader(new FileInputStream(localPath + dirs[i]));
                BufferedReader bufferedReader = new BufferedReader(read);
                String line;
                String[] lineStr;
                int count = 0;
                while((line = bufferedReader.readLine())!=null) {
                    count++;
                    if(count==1){
                        continue;
                    }
                    lineStr = line.split(",",-1);
                    /**处理CcScc业务**/
                    vgcEbmCcScc = AmfFactory.getAmfVO(eventType);
                    vgcEbmCcScc.setEvent_result(resultTypeMap.get(lineStr[0].toUpperCase()));
                    vgcEbmCcScc.setCause_code(Integer.parseInt(lineStr[6]));
                    vgcEbmCcScc.setCause_prot_type(lineStr[7]);
                    vgcEbmCcScc.setSub_cause_code(Integer.parseInt(lineStr[8]));
                    addCcSccList(ccSccList, vgcEbmCcScc, lineStr);
                    /**新增为了处理n_tac和nci统计*/
                    vgcEbmCcScc.setTac(lineStr[9]);
                    vgcEbmCcScc.setNci(lineStr[10]);
                    tacNciList.add(vgcEbmCcScc);
                    /**处理多维业务**/
                    if(lineStr[0].equals("success")){
                        continue;
                    }
                    dimension = AmfFactory.getAmfDimensionVO(eventType);
                    dimension.setEvent_result(resultTypeMap.get(lineStr[0].toUpperCase()));
                    dimension.setDateTime(getTimePoint(lineStr[1],lineStr[2]));
                    dimension.setDT15(getTimePoint15(lineStr[1],lineStr[2]));
                    dimension.setCause_code(Integer.parseInt(lineStr[6]));
                    dimension.setSub_cause_code(Integer.parseInt(lineStr[8]));
                    dimension.setDuration(getDuration(lineStr[5]));
                    dimension.setTac(lineStr[9]);
                    dimension.setNci(lineStr[10]);
                    dimension.setGnbid(getGnbid(lineStr[10]));
                    dimension.setAmfi(getAmfi(lineStr[11],lineStr[12],lineStr[13]));
                    dimension.setImsi(getImsi(lineStr[14]));
                    dimension.setMsisdn(getMsisdn(lineStr[15]));
                    dimension.setImeisv(lineStr[16]);
                    addDimensionList(dimensionList,dimension,lineStr);
                }
                bufferedReader.close();
                read.close();
            }
            String tableName = dateTime.substring(0,10).replaceAll("-", "_") + "_" + netId + "_" +eventType;
            String time = dateTime.substring(11);
            Map<VgcEbmCcScc, Long> ccSccMap = ccSccList.stream().collect(Collectors.groupingBy(x -> x, counting()));
            String[] dimensionArr = ebmParam.getDimensions().get(eventType).split(",");
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
            Map<String, List<Dimension>> chartTableMap = dimensionList.stream().collect(Collectors.groupingBy(x->switchReturnGroupKey(x)));
            for(Map.Entry<String,List<Dimension>> entry:chartTableMap.entrySet()){
                List<Dimension> _dimensionList = entry.getValue();
                this.dealChartTable(_dimensionList, tableName, time);
            }
            this.dealDimensionTable(dimensionList, tableName, time, dimensionArr);
            if(ccSccMap.size()>0){
                //统计前7天的平均值
                this.querySevenAveValue(time, ccSccMap);
                this.insertCcSccTable(ccSccMap,tableName,time);
            }
            //统计TAC和NCI的对应数据并入库
            tacNciStatisticAndInsert(tacNciList,tableName,dateTime);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
            inner_latch.countDown();
        }
    }

    private String switchReturnGroupKey(Dimension d){
        switch(eventType){
            case "amf_deregistration":
                return d.getDeregistration_trigger().toString();
            case "amf_pdu_session_establishment":
                return d.getDnn()+"-"+d.getUe_requested_dnn();
            case "amf_handover":
                return d.getDnn()+"-"+d.getN_handover_type();
            case "amf_service_request":
                return d.getDnn()+"-"+d.getService_request_type()+"-"+d.getService_request_trigger();
            default:
                return "-";
        }
    }

    //统计7天平均值的逻辑
    private void querySevenAveValue(String time, Map<VgcEbmCcScc, Long> ccSccMap) {
        Date date = parseDate(dateTime.substring(0,10));
        List<VgcEbmCcScc> prevSisDays = new ArrayList<>();
        double count = 1;
        for(int i=1;i<7;i++){
            String previousTableName = DateUtils.beforNumDay(date, -i).replaceAll("-", "_") + "_" + netId + "_" +eventType;
            int exist = vgcEbmLogMapper.queryTableExist(previousTableName);
            if(exist == 1){//等于1表示存在
                List<VgcEbmCcScc> currentDayList = this.selectCcSccTable(previousTableName, time);
                prevSisDays.addAll(currentDayList);
                count++;
            }else{//一旦不存在，那再之前的一天肯定也不存在，没有意义，直接跳出循环
                break;
            }
        }
        Map<VgcEbmCcScc, LongSummaryStatistics> collect6 = prevSisDays.stream().collect(Collectors.groupingBy(x -> {
            VgcEbmCcScc vecs = AmfFactory.getAmfVO(eventType);
            vecs.setCause_code(x.getCause_code());
            vecs.setSub_cause_code(x.getSub_cause_code());
            vecs.setDnn(x.getDnn());
            vecs.setN_handover_type(x.getN_handover_type());
            vecs.setDeregistration_trigger(x.getDeregistration_trigger());
            vecs.setService_request_type(x.getService_request_type());
            vecs.setUe_requested_dnn(x.getUe_requested_dnn());
            vecs.setService_request_trigger(x.getService_request_trigger());
            return vecs;
        }, Collectors.summarizingLong(x-> x.getValue())));
        for(Map.Entry<VgcEbmCcScc,Long> entry: ccSccMap.entrySet()){
            if(collect6.containsKey(entry.getKey())){
                Double _ave = (entry.getValue()+collect6.get(entry.getKey()).getSum())/count;
                BigDecimal bd = new BigDecimal(_ave);
                Double ave = bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                entry.getKey().setAve(ave);
            }else{
                entry.getKey().setAve(Double.valueOf(entry.getValue()));
            }
        }
    }

    /**
     * 处理多维业务
     * @param dimensionList
     * @param tableName
     * @param time
     * @param dimensionArr
     */
    private void dealDimensionTable(List<Dimension> dimensionList, String tableName, String time, String[] dimensionArr) {
        for(int i = 0; i< dimensionArr.length; i++){
           String k_name = dimensionArr[i];
            Map<Dimension, Long> dimensionMap = dimensionList.stream().collect(Collectors.groupingBy(x -> {
                Object property = BeanUtil.pojo.getProperty(x, k_name);
                x.setK_key(property.toString());
                return x;
            }, counting()));
            if(dimensionMap.size()>0){
                insertDimensionTable(dimensionMap, tableName +"_dimension", time,k_name);
            }
        }
    }

    /**
     * 三维图表业务
     * @param dimensionList
     * @param tableName
     * @param time
     */
    private void dealChartTable(List<Dimension> dimensionList, String tableName, String time) {
        //先按照具体到毫秒的详细时间排个序
        List<Dimension> sortedList = dimensionList.stream().sorted(Comparator.comparing(x->x.getDateTime())).collect(Collectors.toList());
        Map<String,Dimension> map = new HashMap<>();
        for (int i = 0; i < sortedList.size(); i++) {
            Dimension dim = sortedList.get(i);
            if(map.containsKey(dim.getDateTime())){
                Dimension d = map.get(dim.getDateTime());
                d.setTotal(d.getTotal()+1);
                d.setFailNumber(dim.getEvent_result());
            }else{
                dim.setTotal(1);
                dim.setFailNumber(dim.getEvent_result());
                map.put(dim.getDateTime(),dim);
            }
        }
        List<Dimension> newList;
        if(time.equals("00:00:00")){
            newList = map.values().stream().filter(x->!x.getDateTime().substring(0,2).equals("23")).collect(Collectors.toList());
        }else{
            newList = map.values().stream().collect(Collectors.toList());
        }
        this.insertChartTable(newList,tableName +"_chart",time);
    }

    private void addDimensionList(List<Dimension> dimensionList, Dimension dimension, String[] lineStr) {
        switch(eventType){
            case "amf_initial_registration":
                dimension.setEmergency_registration(lineStr[17]);
                dimension.setSlice_selection_result(lineStr[18]);
                dimensionList.add(dimension);
                break;
            case "amf_service_request":
                String lineDnn3 = lineStr[20].toUpperCase();
                Integer dnn3 = dnnMap.containsKey(lineDnn3)?dnnMap.get(lineDnn3):4;//4为其他
                dimension.setDnn(dnn3);
                dimension.set_dnn(_dnnMap.get(dnn3));
                String lineSrtype  = lineStr[17].toUpperCase();
                Integer srtype = srtypeMap.containsKey(lineSrtype)?srtypeMap.get(lineSrtype):4;//4为其他
                dimension.setService_request_type(srtype);
                dimension.set_service_request_type(_srtypeMap.get(srtype));
                String lineSrtrigger  = lineStr[18].toUpperCase();
                Integer srtrigger = srtriggerMap.containsKey(lineSrtrigger)?srtriggerMap.get(lineSrtrigger):3;//3为其他
                dimension.setService_request_trigger(srtrigger);
                dimension.set_service_request_trigger(_srtriggerMap.get(srtrigger));
                dimension.setRequest_retries(lineStr[19]);
                dimension.setPdu_session_reactivation_result(lineStr[21]);
                dimensionList.add(dimension);
                break;
            case "amf_paging":
                dimension.setPaging_attempts(lineStr[17]);
                dimension.setPaging_trigger(lineStr[18]);
                dimension.setTotal_no_of_paged_ngran_node(lineStr[19]);
                dimensionList.add(dimension);
                break;
            case "amf_pdu_session_establishment":
                String lineDnn1 = lineStr[18].toUpperCase();
                Integer dnn1 = dnnMap.containsKey(lineDnn1)?dnnMap.get(lineDnn1):4;//4为其他
                dimension.setDnn(dnn1);
                dimension.set_dnn(_dnnMap.get(dnn1));
                String lineUeDnn = lineStr[17].toUpperCase();
                Integer ueDnn = dnnMap.containsKey(lineUeDnn)?dnnMap.get(lineUeDnn):4;//4为其他
                dimension.setUe_requested_dnn(ueDnn);
                dimension.set_ue_requested_dnn(_dnnMap.get(ueDnn));
                dimension.setSmf_ipv6(lineStr[19]);
                dimension.setS_nssai(lineStr[20]+"-"+lineStr[21]);
                dimensionList.add(dimension);
                break;
            case "amf_handover":
                String lineDnn2 = lineStr[20].toUpperCase();
                Integer dnn2 = dnnMap.containsKey(lineDnn2)?dnnMap.get(lineDnn2):4;//4为其他
                dimension.setDnn(dnn2);
                dimension.setHandover_type(lineStr[17]);
                dimension.setHandover_rat_change_type(lineStr[18]);
                dimension.setEps_fallback_triggered(lineStr[19]);
                dimension.setOld_gnbid_new_gnbid(lineStr[21]+"->"+lineStr[10]);
                dimension.setOld_tac_new_tac(lineStr[22]+"->"+lineStr[9]);
                dimension.setOld_amfi_new_amfi(getAmfi(lineStr[23],lineStr[24],lineStr[25])+"->"+getAmfi(lineStr[11],lineStr[12],lineStr[13]));
                dimension.setOld_tac_new_eps_tac(lineStr[22]+"->"+lineStr[26]);
                dimension.setOld_amfi_new_mme(getAmfi(lineStr[23],lineStr[24],lineStr[25])+"->"+lineStr[27]);
                if(lineStr[18].equals("5gs_to_eps")||lineStr[18].equals("eps_to_5gs")){
                    dimension.setN_handover_type(handoverMap.get(lineStr[18].toUpperCase()));
                }else if(lineStr[17].equals("n2")||lineStr[17].equals("xn")){
                    dimension.setN_handover_type(handoverMap.get(lineStr[17].toUpperCase()));
                }else{
                    dimension.setN_handover_type(7);
                }
                dimensionList.add(dimension);
                if(lineStr[19].equals("true")){
                    Dimension newVO = new Handover();
                    BeanUtils.copyProperties(dimension,newVO);
                    newVO.setN_handover_type(handoverMap.get(lineStr[19]));
                    dimensionList.add(newVO);
                }
                break;
            case "amf_deregistration":
                String linedTrigger = lineStr[17].toUpperCase();
                Integer trigger = deregistartionMap.containsKey(linedTrigger)?deregistartionMap.get(linedTrigger):4;
                dimension.setDeregistration_trigger(trigger);
                dimension.set_deregistration_trigger(_deregistartionMap.get(trigger));
                dimension.setReregistration_required(lineStr[18]);
                dimensionList.add(dimension);
                break;
            default:
                dimensionList.add(dimension);
        }
    }

    private void addCcSccList(List<VgcEbmCcScc> list,VgcEbmCcScc vgcEbmCcScc,String[] lineStr) {
        switch(eventType){
            case "amf_deregistration":
                String linedTrigger = lineStr[17].toUpperCase();
                Integer trigger = deregistartionMap.containsKey(linedTrigger)?deregistartionMap.get(linedTrigger):4;
                vgcEbmCcScc.setDeregistration_trigger(trigger);
                list.add(vgcEbmCcScc);
                break;
            case "amf_pdu_session_establishment":
                String lineDnn1 = lineStr[18].toUpperCase();
                Integer dnn1 = dnnMap.containsKey(lineDnn1)?dnnMap.get(lineDnn1):4;//4为其他
                vgcEbmCcScc.setDnn(dnn1);

                String lineUeDnn = lineStr[17].toUpperCase();
                Integer ueDnn = dnnMap.containsKey(lineUeDnn)?dnnMap.get(lineUeDnn):4;//4为其他
                vgcEbmCcScc.setUe_requested_dnn(ueDnn);
                list.add(vgcEbmCcScc);
                break;
            case "amf_handover":
                String lineDnn2 = lineStr[20].toUpperCase();
                Integer dnn2 = dnnMap.containsKey(lineDnn2)?dnnMap.get(lineDnn2):4;//4为其他
                vgcEbmCcScc.setDnn(dnn2);

                if(lineStr[18].equals("5gs_to_eps")||lineStr[18].equals("eps_to_5gs")){
                    vgcEbmCcScc.setN_handover_type(handoverMap.get(lineStr[18].toUpperCase()));
                }else if(lineStr[17].equals("n2")||lineStr[17].equals("xn")){
                    vgcEbmCcScc.setN_handover_type(handoverMap.get(lineStr[17].toUpperCase()));
                }else{
                    System.out.println("5gs:"+lineStr[18]+" === n2:"+lineStr[17]);
                    vgcEbmCcScc.setN_handover_type(7);
                }
                list.add(vgcEbmCcScc);

                if(lineStr[19].equals("true")){
                    VgcEbmCcScc newVO = new HandoverCcScc();
                    BeanUtils.copyProperties(vgcEbmCcScc,newVO);
                    newVO.setN_handover_type(handoverMap.get(lineStr[19].toUpperCase()));
                    list.add(newVO);
                }
                break;
            case "amf_service_request":
                String lineDnn3 = lineStr[20].toUpperCase();
                Integer dnn3 = dnnMap.containsKey(lineDnn3)?dnnMap.get(lineDnn3):4;//4为其他
                vgcEbmCcScc.setDnn(dnn3);

                String lineSrtype  = lineStr[17].toUpperCase();
                Integer srtype = srtypeMap.containsKey(lineSrtype)?srtypeMap.get(lineSrtype):4;//4为其他
                vgcEbmCcScc.setService_request_type(srtype);

                String lineSrtrigger  = lineStr[18].toUpperCase();
                Integer srtrigger = srtriggerMap.containsKey(lineSrtrigger)?srtriggerMap.get(lineSrtrigger):3;//3为其他
                vgcEbmCcScc.setService_request_trigger(srtrigger);

                list.add(vgcEbmCcScc);
                break;
            default:
                list.add(vgcEbmCcScc);
        }
    }

    private void insertDimensionTable(Map<Dimension,Long> dimensionMap,String tableName,String time,String k_name){
        switch(eventType){
            case "amf_deregistration":
                vgcEbmLogMapper.insDeregistration(dimensionMap,time,tableName,k_name);
                break;
            case "amf_pdu_session_establishment":
                vgcEbmLogMapper.insPduSessionEstablishment(dimensionMap,time,tableName,k_name);
                break;
            case "amf_handover":
                vgcEbmLogMapper.insHandover(dimensionMap,time,tableName,k_name);
                break;
            case "amf_service_request":
                vgcEbmLogMapper.insServiceRequest(dimensionMap,time,tableName,k_name);
                break;
            default:
                vgcEbmLogMapper.insertDimension(dimensionMap,time,tableName,k_name);
        }
    }

    /**
     * ccScc表统计结果入库
     * @param ccSccMap
     * @param tableName
     * @param time
     */
    private void insertCcSccTable(Map<VgcEbmCcScc, Long> ccSccMap, String tableName, String time) {
        switch(eventType){
            case "amf_deregistration":
                vgcEbmLogMapper.insCcSccDeregistration(ccSccMap,time,tableName);
                break;
            case "amf_pdu_session_establishment":
                vgcEbmLogMapper.insCcSccPduSessionEstablishment(ccSccMap,time,tableName);
                break;
            case "amf_handover":
                vgcEbmLogMapper.insCcSccHandover(ccSccMap,time,tableName);
                break;
            case "amf_service_request":
                vgcEbmLogMapper.insCcSccServiceRequest(ccSccMap,time,tableName);
                break;
            default:
                vgcEbmLogMapper.insertCcScc(ccSccMap,time,tableName);
        }
    }

    private void insertChartTable(List<Dimension> list, String tableName, String time) {
        switch(eventType){
            case "amf_deregistration":
                vgcEbmLogMapper.insChartDeregistration(list,time,tableName);
                break;
            case "amf_pdu_session_establishment":
                vgcEbmLogMapper.insChartPduSessionEstablishment(list,time,tableName);
                break;
            case "amf_handover":
                vgcEbmLogMapper.insChartHandover(list,time,tableName);
                break;
            case "amf_service_request":
                vgcEbmLogMapper.insChartServiceRequest(list,time,tableName);
                break;
            default:
                vgcEbmLogMapper.insertChartDefault(list,time,tableName);
        }
    }

    private List<VgcEbmCcScc> selectCcSccTable(String previousTableName, String time) {
        switch(eventType){
            case "amf_deregistration":
                return vgcEbmLogMapper.selectDeregistration(previousTableName, time);
            case "amf_pdu_session_establishment":
                return vgcEbmLogMapper.selectPduSessionEstablishment(previousTableName, time);
            case "amf_handover":
                return vgcEbmLogMapper.selectHandover(previousTableName, time);
            case "amf_service_request":
                return vgcEbmLogMapper.selectServiceRequest(previousTableName, time);
            default:
                return vgcEbmLogMapper.selectCcSccTable(previousTableName, time);
        }
    }

    /**
     * 统计tac和nci的对应数据
     * @param list
     * @param tableName
     * @param dateTime
     */
    private void tacNciStatisticAndInsert(List<VgcEbmCcScc> list,String tableName,String dateTime) {
        List<TacVO> tacList = new ArrayList<>();
        Map<String, List<VgcEbmCcScc>> tacMap = list.stream().collect(Collectors.groupingBy(VgcEbmCcScc::getTac));
        TacVO tacVO;
        for(Map.Entry<String,List<VgcEbmCcScc>> entry:tacMap.entrySet()){
            tacVO = new TacVO();
            List<VgcEbmCcScc> currentTacList = entry.getValue();
            Map<Integer, Long> map = currentTacList.stream().collect(
                    Collectors.groupingBy(VgcEbmCcScc::getEvent_result,counting()));
            tacVO.setTac(entry.getKey());
            tacVO.setTotal_count(map.values().stream().reduce(0L, Long::sum).intValue());
            tacVO.setFailure_count(tacVO.getTotal_count()-(map.containsKey(1)?map.get(1).intValue():0));
            if(tacVO.getTotal_count()!=0) {
                double dout = (map.containsKey(1)?map.get(1).intValue():0)/(double)tacVO.getTotal_count();
                BigDecimal bd = new BigDecimal(dout);
                bd = bd.setScale(6,BigDecimal.ROUND_HALF_UP);
                tacVO.setSuccess_rate(bd.doubleValue()*100);
            }
            tacList.add(tacVO);
        }
        vgcEbmLogMapper.insertTacTable(tacList,dateTime,tableName+"_n_tac");
        List<NciVO> nciList = new ArrayList<>();
        Map<String, List<VgcEbmCcScc>> nciMap = list.stream().collect(Collectors.groupingBy(VgcEbmCcScc::getNci));
        NciVO nciVO;
        for(Map.Entry<String,List<VgcEbmCcScc>> entry:nciMap.entrySet()){
            nciVO = new NciVO();
            List<VgcEbmCcScc> currentNciList = entry.getValue();
            Map<Integer, Long> map = currentNciList.stream().collect(
                    Collectors.groupingBy(VgcEbmCcScc::getEvent_result,counting()));
            nciVO.setNci(entry.getKey());
            nciVO.setGnbid(getGnbid(entry.getKey()));
            nciVO.setTotal_count(map.values().stream().reduce(0L, Long::sum).intValue());
            nciVO.setFailure_count(nciVO.getTotal_count()-(map.containsKey(1)?map.get(1).intValue():0));
            if(nciVO.getTotal_count()!=0) {
                double dout = (map.containsKey(1)?map.get(1).intValue():0)/(double)nciVO.getTotal_count();
                BigDecimal bd = new BigDecimal(dout);
                bd = bd.setScale(6,BigDecimal.ROUND_HALF_UP);
                nciVO.setSuccess_rate(bd.doubleValue()*100);
            }
            nciList.add(nciVO);
        }
        vgcEbmLogMapper.insertNciTable(nciList,dateTime,tableName+"_nci");
    }
}
